package elEMAS.impl;

import java.util.Properties;

import common.CommonHelper;
import common.GeneticHelper;
import common.GeneticOperations;

public abstract class ElEmasRunner<Genotype> {
    
    public abstract GeneticOperations<Genotype> getGeneticOperations(Properties properties, EmasParameters params);
    
    public String getPrefix(String inputFile, String problemName) {
        
        String prefix = CommonHelper.fromInputfilenameToPrefix(inputFile);
        prefix = "output/"+prefix+"_elEMAS_";
        return prefix;
    }
    
    public void start(String problemName, String[] args) {
        
        CommonHelper.printAssertionState();
        System.out.println("elEMAS "+problemName+" Witaj !!!");
        System.out.println();
        
        if (args.length != 1) {
            System.out.println("Parametry: [plik_properties]");
            System.exit(1);
        } else {
            System.out.println("Wczytuje properties: "+args[0]);
        }
        
        Properties properties = CommonHelper.readProperties(args[0]);
        ElEmasParameters params = ElEmasHelpers.getElEmasParameters(properties);
        
        GeneticOperations<Genotype> go = getGeneticOperations(properties, params);
        
        ElEmasAlgorithm<Genotype> er = new ElEmasAlgorithm<Genotype>(go,params);
        String prefix = getPrefix(params.inputFile,problemName.toLowerCase());
        System.out.println("prefix dla plików wynikowych: "+prefix);
        er.printParameters();
        
        CommonHelper.printToFile(prefix+"random.txt", GeneticHelper.populationToString(er.getAllAgents()));
        
        CommonHelper.sleep(1000);
        er.run();
        er.printResultStatistics();
        
        String HvInTime = CommonHelper.HvInTimeToStr(er.getHvInTime());
        CommonHelper.printToFile(prefix+"hv.txt", HvInTime);
        
        GeneticHelper.savePopulationToFile(prefix+"elit",er.getElitists());
        GeneticHelper.savePopulationToFile(prefix+"all",er.getAllAgents());
    }
    
}